{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 摄像头动态检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ WARN:0@162.504] global cap_v4l.cpp:997 open VIDEOIO(V4L2:/dev/video0): can't open camera by index\n",
      "[ERROR:0@162.504] global obsensor_uvc_stream_channel.cpp:159 getStreamChannelGroup Camera index out of range\n"
     ]
    },
    {
     "ename": "error",
     "evalue": "OpenCV(4.9.0) /io/opencv/modules/highgui/src/window.cpp:1266: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvDestroyAllWindows'\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[11], line 29\u001b[0m\n\u001b[1;32m     25\u001b[0m         \u001b[38;5;28;01mbreak\u001b[39;00m\n\u001b[1;32m     28\u001b[0m video\u001b[38;5;241m.\u001b[39mrelease()  \u001b[38;5;66;03m# 释放摄像头\u001b[39;00m\n\u001b[0;32m---> 29\u001b[0m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdestroyAllWindows\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# 销毁窗口\u001b[39;00m\n\u001b[1;32m     30\u001b[0m cv2\u001b[38;5;241m.\u001b[39mwaitKey(\u001b[38;5;241m1\u001b[39m) \u001b[38;5;66;03m# \u001b[39;00m\n",
      "\u001b[0;31merror\u001b[0m: OpenCV(4.9.0) /io/opencv/modules/highgui/src/window.cpp:1266: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvDestroyAllWindows'\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "\n",
    "video = cv2.VideoCapture(0)  # 0表示打开笔记本的内置摄像头\n",
    "\n",
    "#cv2.namedWindow('video') # 创建窗口\n",
    "\n",
    "mog = cv2.createBackgroundSubtractorMOG2() # 创建背景减除器\n",
    "while(True):\n",
    "    ret, frame = video.read() # 读取摄像头\n",
    "    if ret:\n",
    "        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转换为灰度图\n",
    "        gray =cv2.flip(gray, 1) # 1水平翻转,0 \n",
    "        blue = cv2.GaussianBlur(gray, (5, 5), 5) # 高斯模糊\n",
    "        fgMask = mog.apply(blue) # 背景减除\n",
    "\n",
    "        erode = cv2.erode(fgMask, (5,5), iterations=1)\n",
    "        dialte = cv2.dilate(erode, (5,5), iterations=1)\n",
    "     #   cv2.imshow('video',erode) # 显示一帧图像\n",
    "        cv2.imshow('video1',dialte) # 显示一帧图像\n",
    "\n",
    "        key = cv2.waitKey(33) # 每33 ms显示一帧图像\n",
    "        if(key == 27): # 27表示esc键 退出\n",
    "            break\n",
    "    else:\n",
    "        break\n",
    "\n",
    "\n",
    "video.release()  # 释放摄像头\n",
    "cv2.destroyAllWindows() # 销毁窗口\n",
    "cv2.waitKey(1) # "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
